#pragma once

class BoxCenterAndExtent
{
public:
	Vector4 Center;
	Vector4 Extent;

	/** Default constructor. */
	BoxCenterAndExtent() {}

	/** Initialization constructor. */
	BoxCenterAndExtent(const Vector3& InCenter,const Vector3& InExtent)
		:	Center(InCenter.X, InCenter.Y, InCenter.Z,0)
		,	Extent(InExtent.X, InExtent.Y, InExtent.Z,0)
	{}

	/** AxisAlignedBox conversion constructor. */
	BoxCenterAndExtent(const AxisAlignedBox& BoxShape)
	{
		Center = BoxShape.getCenter();
		Extent = BoxShape.getHalfSize();

		Center.W = Extent.W = 0;
	}

	/** Converts to a AxisAlignedBox. */
	AxisAlignedBox GetBox() const
	{
		Vector4 min_pos(Center - Extent), max_pos(Center + Extent);
		return AxisAlignedBox(min_pos.X, min_pos.Y, min_pos.Z, max_pos.X, max_pos.Y, max_pos.Z);
	}

	/**
	 * Determines whether two boxes intersect.
	 * @return true if the boxes intersect, or false.
	 */
	friend __forceinline bool Intersect(const BoxCenterAndExtent& A,const BoxCenterAndExtent& B)
	{
		Vector4 CenterA(A.Center), CenterB(B.Center), ExtentA(A.Extent), ExtentB(B.Extent);

		// CenterDifference is the vector between the centers of the bounding boxes.
		const VectorRegister CenterDifference = VectorAbs(VectorSubtract(VectorLoadAligned(&CenterA),VectorLoadAligned(&CenterB)));

		// CompositeExtent is the extent of the bounding box which is the convolution of A with B.
		const VectorRegister CompositeExtent = VectorAdd(VectorLoadAligned(&ExtentA),VectorLoadAligned(&ExtentB));

		// For each axis, the boxes intersect on that axis if the projected distance between their centers is less than the sum of their
		// extents.  If the boxes don't intersect on any of the axes, they don't intersect.
		return VectoryAnyGreaterThan(CenterDifference,CompositeExtent) == false;
	}
};

